package main

import (
	"context"
	"fmt"
	"log"
	pkg "operator-crd/pkg/controllers"
	"operator-crd/pkg/generated/clientset/versioned"
	"operator-crd/pkg/generated/informers/externalversions"

	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/tools/clientcmd"
)

func main() {
	config, err := clientcmd.BuildConfigFromFlags("", clientcmd.RecommendedHomeFile)
	if err != nil {
		log.Fatalln(err)
	}
	c, err := versioned.NewForConfig(config)
	if err != nil {
		log.Fatalln(err)
	}

	cks, err := c.BseipV1().ClickHouses("default").List(context.TODO(), v1.ListOptions{})
	if err != nil {
		log.Fatalln(err)
	}
	for _, ck := range cks.Items {
		fmt.Println(ck.Name)
	}

	factory := externalversions.NewSharedInformerFactory(c, 0)
	ifm := factory.Bseip().V1().ClickHouses()

	controller := pkg.NewController(*c, ifm)
	stopCh := make(chan struct{})
	factory.Start(stopCh)
	factory.WaitForCacheSync(stopCh)

	controller.Run(stopCh)
}
